<?php

/**
 * 工具类
 */
class Utils {

    /**
     * 时间格式化
     * @param int $timestamp 时间戳
     * @param string $format 格式
     * @param bool $human 是否人性化
     */
    public static function sDate($timestamp, $format='', $human=0) {
        global $_SCONFIG;
        $format = $format ? $format : $_SCONFIG['dateformat'];
        return date($format, $timestamp);
    }

    /**
     * 取得微秒时间
     */
    public static function getMicroTime() {
        list($usr, $sec) = explode(" ", microtime());
        return ((float) $usr + (float) $sec);
    }

    /**
     * 获取表名
     * @param string $table
     */
    public static function tname($table) {
        global $_SC;
        return $_SC['tablepre'] . $table;
    }

    /**
     * 从一个数组中删除一个元素
     * @param $array 要删除的数组
     * @param $element 要删除的元素
     */
    public static function removeFromArray($array, $element) {
        $func = create_function('$a', "if(\$a==$element)return false;else return true;");
        return array_filter($array, $func);
    }

    /**
     * html过滤
     * @param string $string
     */
    public static function shtmlspecialchars($string) {
        $search = array('&', '<', '>');
        $replace = array('&amp;', '&lt;', '&gt;');
        $string = str_replace($search, $replace, $string);
        return $string;
    }

    /**
     * 取得的ip地址
     * @param bool $format 是否格式化
     */
    public static function getonlineip() {
        global $_SGLOBAL;
        if (!isset($_SGLOBAL['onlineip'])) {
            if (isset($_SERVER['REMOTE_ADDR'])) {
                $onlineip = $_SERVER['REMOTE_ADDR'];
            }
        }
        $_SGLOBAL['onlineip'] = $onlineip;
        return $onlineip;
    }

    /**
     * 字符串转义
     * @param max $str
     */
    public static function saddslashes($str) {
        if (is_array($str)) {
            foreach ($str as &$s) {
                $s = addslashes($s);
            }
            return $str;
        } else {
            return addslashes($str);
        }
    }

    /**
     * 将十六进制转换为二进制
     * @param string $hexdata 十六进制数据
     * @return byte bindata 二进制数据
     */
    public static function hex2bin($hexdata) {
        $bindata = '';
        $length = strlen($hexdata);
        for ($i = 0; $i < $length; $i += 2) {
            $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
        }
        return $bindata;
    }

    /**
     * 将数组保存到文件中
     * @param string $variable 变量名
     * @param array $array 数组
     * @param string $file 要保存的文件名
     */
    public static function arrayTOFile($variable, $array, $file) {
        if (is_array($array)) {
            $crlf = (PHP_OS == 'WINNT' ? "\r\n" : "\n");
            $string = self::arrayToString($array);
            $string = "<?php$crlf\$$variable=" . $string . ";$crlf?>";
            file_put_contents($file, $string);
        }
    }

    /**
     * 将数组转换成字符串
     * @param array $array 数组
     */
    private static function arrayToString($array, $level=0) {
        $padding = str_pad("\t", $level + 1, "\t");
        $crlf = (PHP_OS == 'WINNT' ? "\r\n" : "\n");

        $evaluate .= "array($crlf";
        foreach ($array as $key => $value) {
            $evaluate.=$padding . (is_numeric($key) ? $key : "'" . addslashes($key) . "'");
            $evaluate.='=>';
            if (is_numeric($value)) {
                $evaluate.=$value;
            } elseif (is_array($value)) {
                $evaluate.=self::arrayToString($value, $level + 1);
            } else {
                $evaluate.="'" . addslashes($value) . "'";
            }
            $evaluate.=",$crlf";
        }
        $evaluate.= ( !$level ? '' : $padding) . ")";
        return $evaluate;
    }

    /**
     * 取得用户的sid
     */
    public static function getSid() {
        return uniqid();
    }

    /**
     * 生成formhash
     */
    public static function formhash() {
        global $_SGLOBAL;
        return substr(md5($_SERVER['HTTP_USER_AGENT']) . Utils::getonlineip(), 0, 16);
    }

    /**
     * 表单提交检查
     * @param string $submit 要检查的变量
     * @param string $method 要检查的提交方法
     */
    public static function submitCheck($submit) {
        if (!isset($_REQUEST[$submit])) {
            return false;
        } else {
            if (isset($_REQUEST['formhash']) && $_REQUEST['formhash'] == self::formhash()) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 现实信息
     * @param string $message 信息内容
     * @param string $returnUrl 返回的地址
     * @param int $timeout 返回时间
     */
    public static function showMessage($message, $returnUrl='', $timeout=3) {
        global $_SGLOBAL;
        if ($_SGLOBAL['inajax']) {
            $message = "<root>" . ($message == 'do_success' ? '<ajaxok/>' : '') . self::lang($message) . "</root>";
            echo $message;
        } else {
            $_SGLOBAL['view']->assign("refer",$returnUrl);
            $_SGLOBAL['view']->assign('timeout',$timeout*1000);
            $_SGLOBAL['view']->assign('message', self::lang($message));
            $_SGLOBAL['view']->display('showmessage');
        }
        exit;
    }

    public static function lang($message) {
        static $lang = array();
        if (empty($lang)) {
            include_once S_ROOT . '/template/default/template.lang.php';
        }
        return isset($lang[$message]) ? $lang[$message] : $message;
    }

}

?>
